
$(function () {
    var id=getvl("id");
    if(id!=""){
        loadData(id);
    }
});

//显示数据(修改)
function loadData(id) {
    $.ajax({
        type: "GET",
        url: urlcore + "/api/deliveryPlan/selectOne?id=" + id, //图片上传出来的url，返回的是图片上传后的路径，http格式
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: {},
        success: function (data) {
            var da = data.data;
            console.log(da);
            if (data.success == true) {
                //处理规格和日期
                $("#title").val(da.title);
                for(var item of da.deliveryItems){
                    var trObj=$("#deliTable").find('tr[areaNo="'+item.areaNo+'"]');
                    trObj.attr("data-id",item.id);
                    trObj.find("input[name='firstPostage']").val(item.firstPostage);
                    trObj.find("input[name='continuePostage']").val(item.continuePostage);
                }
            } else {
                alert(data.msg);
            }
        },
        error: function () {
            alert("失败");
        }
    });
}


//防止短时间内多次点击，防抖
var clickTag=true;
function save() {
    if(!clickTag){
        alert("正在保存中！");
        return;
    }

    var id=getvl("id");
    var title = $("#title").val();


    if (!validForm()) {
        alert("请完善必填信息");
        return;
    }


    console.log("title:", title);


    //方案子项
    var deliItems = [];
    //sku
    $("#deliTable > tbody tr").each(function () {
        //地区编号
        var itemId=$(this).attr("data-id");
        var areaNo = $(this).attr("areaNo");
        var firstPostage = $(this).find("input[name='firstPostage']").val();
        var continuePostage = $(this).find("input[name='continuePostage']").val();
        deliItems.push({
            id:itemId,
            areaNo: areaNo,
            firstPostage: firstPostage,
            continuePostage: continuePostage,
        });
    });

    //方案基本信息
    var postData={
        id:id,
        title:title,
        deliveryItems:deliItems
    };
    clickTag=false;
    $.ajax({
        type: "POST",
        url: urlcore + "/api/deliveryPlan/add",
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(postData),
        success: function (data) {
            console.log(data.data);
            clickTag=true;
            alert("添加成功！");
            window.location.href="delivery_plan_list.html";
        },
        error: function () {
            clickTag=true;
            alert("error")
        }
    });

    //校验表单
    function validForm() {
        var result = true;
        var validateSku = $("#plan-form").find("input");
        var vlength = validateSku.length;
        for (var index = 0; index < vlength; index++) {
            var zz=$(validateSku[index]);
            console.log(validateSku[index]);
            console.log(zz.attr("zz"));
            if (validateSku[index].value == "") {
                return false;
            }
        }
        return result;
    }
}


//保存方案子项
function saveItems(planNum) {


    $.ajax({
        type: "POST",
        url: urlcore + "/api/deliveryPlan/add",
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(deliItems),
        success: function (data) {
            console.log(data.data);

            clickTag=true;
            alert("添加成功！");
            window.location.href="sysmsg_list.html";
        },
        error: function () {
            clickTag=true;
            alert("error")
        }
    });

}